﻿using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Security.Claims;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace Back_end.Models
{
    // 可以通过向 ApplicationUser 类添加更多属性来为用户添加配置文件数据。若要了解详细信息，请访问 http://go.microsoft.com/fwlink/?LinkID=317594。
    public class ApplicationUser : IdentityUser
    {
		/// <summary>
		/// 用于显示
		/// </summary>
		[Required(ErrorMessage = "必须有一个用户名")]
		[Display(Name = "用户名")]
		[StringLength(16, ErrorMessage = "{0}长度应该在{2}到{1}之间", MinimumLength = 1)]
		public override string UserName
		{ get; set; }

		[Display(Name = "真实姓名")]
		[HiddenInput(DisplayValue = false)]
		public string RealName
		{ get; set; }

		/// <summary>
		/// 头像存放路径
		/// </summary>
		[HiddenInput(DisplayValue = false)]
		public string AvatarPath
		{ get; set; }

		/// <summary>
		/// 未通过实名认证时不能发布举报
		/// </summary>
		[Display(Name = "实名认证状态")]
		[HiddenInput(DisplayValue = false)]
		public bool HadAuthentication
		{ get; set; }

		[Required]
		[Display(Name = "性别")]
		public string Gender
		{ get; set; }

		[Required]
		[Display(Name = "所在地")]
		public string Location
		{ get; set; }

		/// <summary>
		/// 注册日期
		/// </summary>
		[Display(Name = "注册日期")]
		public DateTime RegisterDate
		{ get; set; }

		/// <summary>
		/// 最后一次发布日期
		/// </summary>
		[Display(Name = "最近举报日期")]
		[HiddenInput(DisplayValue = false)]
		public DateTime? LastestDate
		{ get; set; }

		/// <summary>
		/// 每日发布数量
		/// </summary>
		[Display(Name = "今日举报数量")]
		[HiddenInput(DisplayValue = false)]
		public int TodayCount
		{ get; set; }

		/// <summary>
		/// 历史累计积分
		/// </summary>
		[Display(Name = "总共积分")]
		public double TotleCredit
		{ get; set; }

		/// <summary>
		/// 当前可用积分
		/// </summary>
		[Display(Name = "可用积分")]
		public double Credit
		{ get; set; }

		/// <summary>
		/// 发送的举报
		/// </summary>
		public virtual List<Complaint> Complaints
		{ get; set; }

		/// <summary>
		/// 关注的举报，内容为Id
		/// </summary>
		//public virtual List<string> Followed
		//{ get; set; }

		/// <summary>
		/// 点赞
		/// </summary>
		//public virtual List<string> Likes
		//{ get; set; }

		public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
        {
            // 请注意，authenticationType 必须与 CookieAuthenticationOptions.AuthenticationType 中定义的相应项匹配
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
            // 在此处添加自定义用户声明
            return userIdentity;
        }

		public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager, string authenticationType)
		{
			// 请注意，authenticationType 必须与 CookieAuthenticationOptions.AuthenticationType 中定义的相应项匹配
			var userIdentity = await manager.CreateIdentityAsync(this, authenticationType);
			// 在此处添加自定义用户声明
			return userIdentity;
		}
	}

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("AzureDbConnection", throwIfV1Schema: false)
        {
        }

        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }

		public DbSet<Complaint> Complaints
		{ get; set; }

		public DbSet<News> News
		{ get; set; }

		public DbSet<Company> Companies
		{ get; set; }

		public DbSet<FollowedUser> FollowedList
		{ get; set; }

		public DbSet<LikedUser> Likes
		{ get; set; }

		public DbSet<Community> Communities
		{ get; set; }
	}
}